package com.Final.hj;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import io.socket.client.Socket;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String STATE_FRAGMENT_SHOW = "exit_fragment";
    private FragmentManager fragmentManager;
    private Fragment currentFragment;
      //三个fragment
    private SocketIoFragment socket_fragment;
    private FriendNew new_fragment;
    private PersonSetting person_fragment;

    private RelativeLayout rl_content;
      private ImageView item1_iv,item2_iv,item3_iv;
      private TextView item1_tv,item2_tv,item3_tv;
      private LinearLayout item1,item2,item3;
      private ImageView[] ivs;
      private TextView[] tvs;

    private Socket socket;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.home_main);

         //初始化变量
         initView();
         //创建 FragmentManager实例
         fragmentManager = getSupportFragmentManager();

         instanceState(savedInstanceState);
     }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //“内存重启”时保存当前的fragment名字
        outState.putString(STATE_FRAGMENT_SHOW, currentFragment.getClass().getName());
        super.onSaveInstanceState(outState);
    }

    //初始化变量
     private void initView() {
         rl_content = (RelativeLayout) findViewById(R.id.rl_content);

         //分别获取文字和图片 以及它们的LinearLayout区域

         item1_iv = (ImageView) findViewById(R.id.item1_iv);
         item1_tv = (TextView) findViewById(R.id.item1_tv);
         item1 = (LinearLayout) findViewById(R.id.item1);

         item2_iv = (ImageView) findViewById(R.id.item2_iv);
         item2_tv = (TextView) findViewById(R.id.item2_tv);
         item2 = (LinearLayout) findViewById(R.id.item2);

         item3_iv = (ImageView) findViewById(R.id.item3_iv);
         item3_tv = (TextView) findViewById(R.id.item3_tv);
         item3 = (LinearLayout) findViewById(R.id.item3);

         //三个fagment
//         socket_fragment = new SocketIoFragment();
//         new_fragment = new FriendNew();
//         person_fragment = new PersonSetting();

         //改变选中图片和文字颜色
         ivs = new ImageView[]{item1_iv,item2_iv,item3_iv};
         tvs = new TextView[]{item1_tv,item2_tv,item3_tv};

         item1.setOnClickListener(this);
         item2.setOnClickListener(this);
         item3.setOnClickListener(this);
     }
     @Override
     public void onClick(View view) {
         switch (view.getId()){
                 case R.id.item1: {
                     showFragment(socket_fragment, 0);
                         break;
                    }
                 case R.id.item2: {
                     showFragment(new_fragment, 1);
                         break;
                     }
                case R.id.item3: {
                    showFragment(person_fragment, 2);
                        break;
                     }
                default:
                    break;
             }
     }

    @Override
    protected void onRestart() {
        super.onRestart();
        Toast.makeText(HomeActivity.this, "活过来了",Toast.LENGTH_SHORT).show();
    }

    //fragment的切换
    public void showFragment(Fragment fg, int itemId){

        FragmentTransaction transaction = fragmentManager.beginTransaction();//创建一个事务
        if(!fg.isAdded()){//没有添加过
            transaction
                    .hide(currentFragment)//第三个参数为当前的fragment绑定一个tag，tag为当前绑定fragment的类名
                    .add(R.id.rl_content, fg, fg.getClass().getName());
        }else{
            transaction
                    .hide(currentFragment)
                    .show(fg);//添加过了直接显示即可
        }

        //记录当前fragment
        currentFragment = fg;
        transaction.commit();//事务一定要提交，replace才会有效

        //这个方法设置底部导航栏选中时的效果
        for (int i = 0; i < 3; i++) {
            ivs[i].setColorFilter(Color.parseColor("#0f0f0f"));
            tvs[i].setTextColor(Color.parseColor("#0f0f0f"));
        }

        ivs[itemId].setColorFilter(Color.GREEN);
        tvs[itemId].setTextColor(Color.GREEN);
     }
    //解决内存重启问题
    public void instanceState(Bundle savedInstanceState){
        // “内存重启”时调用
        if (savedInstanceState != null) {
            Toast.makeText(HomeActivity.this, "有", Toast.LENGTH_SHORT).show();
            //获取“内存重启”时保存的fragment名字
            String saveFragmentName = savedInstanceState.getString(STATE_FRAGMENT_SHOW);
            //从fragmentManager里面找到fragment
            socket_fragment = (SocketIoFragment) fragmentManager.findFragmentByTag(SocketIoFragment.class.getName());
            new_fragment = (FriendNew) fragmentManager.findFragmentByTag(FriendNew.class.getName());
            person_fragment = (PersonSetting) fragmentManager.findFragmentByTag(PersonSetting.class.getName());

            //说明不是在fragment内存重启的
            if(TextUtils.isEmpty(saveFragmentName)){
                //解决重叠问题show里面可以指定恢复的页面
                fragmentManager.beginTransaction()
                        .show(socket_fragment)
                        .hide(new_fragment)
                        .hide(person_fragment)
                        .commit();

                //把当前显示的fragment记录下来
                currentFragment = socket_fragment;
            }else{
                showExitFragment(saveFragmentName);
            }
        }else{
            //正常启动时调用 没有内存重启
            socket_fragment = new SocketIoFragment();
            new_fragment = new FriendNew();
            person_fragment= new PersonSetting();
            //默认显示第一个
            currentFragment = new EmptyFragment();
            showFragment(socket_fragment, 0);
        }
    }

    public void showExitFragment(String saveFragmentName){
        if(saveFragmentName.equals(socket_fragment.getClass().getName())){
            //解决重叠问题
            fragmentManager.beginTransaction()
                    .show(socket_fragment)
                    .hide(new_fragment)
                    .hide(person_fragment)
                    .commit();

            //把当前显示的fragment记录下来
            currentFragment = socket_fragment;

        }else if(saveFragmentName.equals(new_fragment.getClass().getName())){  //如果推出之前是TwoFragment

            //解决重叠问题
            fragmentManager.beginTransaction()
                    .show(new_fragment)
                    .hide(socket_fragment)
                    .hide(person_fragment)
                    .commit();

            //把当前显示的fragment记录下来
            currentFragment = new_fragment;

        }else{    //如果推出之前是ThreeFragment

            //解决重叠问题
            fragmentManager.beginTransaction()
                    .show(person_fragment)
                    .hide(new_fragment)
                    .hide(socket_fragment)
                    .commit();

            //把当前显示的fragment记录下来
            currentFragment = person_fragment;

        }
    }
}
